package com.itdfq.delay.message.provider;

import com.alibaba.fastjson.JSON;
import com.itdfq.delay.constant.RabbitMqConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: QianMo
 * @Date: 2021/10/19 18:48
 * @Description:
 */
@Component
@Slf4j
public class SendService {


    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * @param msg 消息
     *            固定延时10秒
     */
    public void send2(String msg) {
        log.info("RabbitSender.send() msg = {}", msg);
        rabbitTemplate.convertAndSend(RabbitMqConstant.DELAY_EXCHANGE, RabbitMqConstant.DELAY_ROUTING_KEY, msg);
    }

    /**
     * 可变延时消费
     * @param msg  消息
     * @param expiration 延时时间单位 ： 毫秒
     */
    public void send(String msg,Integer expiration){
        rabbitTemplate.convertAndSend(RabbitMqConstant.DELAY_VARIABLE_EXCHANGE_KEY,
                RabbitMqConstant.DELAY_VARIABLE_ROUTING_KEY, msg,
                message -> {
                    log.info("可变延时消费发送消息: {}, and expiration in {}ms", msg, expiration);
                    message.getMessageProperties().setExpiration(expiration.toString());
                    return message;
                });
    }

}
